package com.software.date10_31;

import java.util.*;

public class test {
    public static void main(String[] args) {
        //set具有天然去重效果
        //底层是Hash表实现的
        HashSet<String> set = new HashSet<>();
        set.add(null);
        set.add("hi");
        set.add("jjy");
        set.add("apple");
        set.add("kiku");
        set.add("hi");
        set.add(null);
        //set通过迭代遍历
        Iterator<String> iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println("===========");
        //例子：集合去重
        List<Integer> list = new ArrayList<>();
        list.add(9);
        list.add(8);
        list.add(8);
        list.add(3);
        list.add(2);
        list.add(2);
        list.add(6);
        for(Integer x: list){
            System.out.print(x + " ");
        }
        System.out.println("去重后：");
        Set<Integer> hashSet = new HashSet<>();
        hashSet.addAll(list);
        for(Integer x: hashSet){
            System.out.print(x + " ");
        }
        System.out.println();
        System.out.println("TreeSet按内容值进行升序排列，内容不能为null");
        Set<String> treeSet = new TreeSet<>();
        treeSet.add("haha");
        treeSet.add("banan");
        treeSet.add("haha");
        treeSet.add("apple");
        Iterator<String> iterator1 = treeSet.iterator();
        while(iterator1.hasNext()){
            System.out.println(iterator1.next());
        }
        System.out.println("LinkedHashSet具有双向链表的特性,LinkedHashSet的迭代器可以按照元素被插入的顺序进行遍历");
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();

        linkedHashSet.add("haha");
        linkedHashSet.add(null);
        linkedHashSet.add("banan");
        linkedHashSet.add("milk");
        linkedHashSet.add(null);
        Iterator<String> iterator2 = linkedHashSet.iterator();
        while(iterator2.hasNext()){
            System.out.println(iterator2.next());
        }
    }
    public static void main2(String[] args) {
        LinkedList list = new LinkedList();
        list.add("kiku");
        list.addFirst(666);
        list.addLast(new Date());
        for(Object x: list){
            System.out.println(x );
        }
        LinkedList<String> l1 = new LinkedList<>();
        l1.addFirst("haha");
        List<String> l2 = new LinkedList<>();
    }
    public static void main1(String[] args) {
        ArrayList a1 = new ArrayList();
        a1.add("kiku");
        a1.add(new Date());
        a1.add(999);
        a1.add("kiku");
        System.out.println(a1.size());
        for(int i = 0; i < a1.size(); i++){
            System.out.println(a1.get(i));
        }
        for(Object x: a1){
            System.out.println(x);
        }
        System.out.println("======================");
        a1.remove(2);
        for(Object x: a1){
            System.out.println(x);
        }
        System.out.println("===============");
        a1.remove("kiku");//移除遇到的第一个"kiku"
        for(Object x: a1){
            System.out.println(x);
        }
    }
}
